//
//  ViewController.m
//  09_Homework
//
//  Created by 徐昊 on 16/7/26.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "City.h"
#import "Province.h"
#import "Zone.h"
#import "VCOfCity.h"
#define kTabViewName @"reuseIndentifier"
@interface ViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *provinceOfTableView;
@property (nonatomic, strong) NSMutableArray *allProArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"省份";
    [self data];
    _provinceOfTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    [self.view addSubview:_provinceOfTableView];
    _provinceOfTableView.delegate = self;
    _provinceOfTableView.dataSource = self;
//    [_provinceOfTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kTabViewName];
}
- (void)data {
    // 取到地址
    NSString *path = @"/Users/xuhao/DLS160710/UI/HomeWork/09_Homework/09_Homework/area.txt";
    // 取到字符串
    NSString *sourceString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    // 取出(根据"\n")截取的所有的字符串, 放入数组中
    NSArray *allArray = [sourceString componentsSeparatedByString:@"\n"];
    // 1. 省数组 - 省字典 - 市数组 - 市字典 - 区数组 - 区字典
    self.allProArray = [NSMutableArray array];
    for (NSString *area in allArray) {
        if (![area hasPrefix:@" "]) {
            Province *pro = [Province zoneWithName:area];
            [_allProArray addObject:pro];
        } else if ([area hasPrefix:@"  "] && ![area hasPrefix:@"    "]) {
            Province *pro = [_allProArray lastObject];
            City *city = [City zoneWithName:area];
            [pro.array addObject:city];
        } else {
            City *city = [[[_allProArray lastObject] array] lastObject];
            Zone *zone = [Zone zoneWithName:area];
            [city.array addObject:zone];
        }
    }
    NSLog(@"%@", _allProArray);
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allProArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kTabViewName];
    NSString *str = [_allProArray[indexPath.row] name];
    NSArray *allArray = [str componentsSeparatedByString:@" "];
    cell.textLabel.text = allArray.firstObject;
    cell.detailTextLabel.text = allArray.lastObject;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    VCOfCity *vc = [[VCOfCity alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    vc.cityArray = [_allProArray[indexPath.row] array];
    NSString *str = [_allProArray[indexPath.row] name];
    NSArray *allArray = [str componentsSeparatedByString:@" "];
    vc.name = allArray.firstObject;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
